//
//  ShopViewController.m
//  1243
//
//  Created by 杨森 on 16/4/13.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShopViewController.h"
#import "Masonry.h"
#import "NetWorkTools.h"
#import "rightShopTableViewCell.h"
#import "ShoeBagDressViewController.h"
#import "SweepViewController.h"
#import "ErWeiMaViewController.h"
#import "JudgeUserLogInTool.h"
#import "ShopCartViewController.h"
#import "NetWorkTools.h"
#import "rightNewShopTableViewCell.h"

#define TableViewHeaderHeight [[UIScreen mainScreen] bounds].size.height/5

@interface ShopViewController ()<UITableViewDelegate,UITableViewDataSource,rightNewShopDelegate>

@property(nonatomic,strong)UITableView *leftTableView;
@property(nonatomic,strong)UITableView *rightTableView;

@property(nonatomic,strong)NSArray *leftArray;
@property(nonatomic,strong)NSArray *rightArray;


@property(nonatomic)NSIndexPath *leftIndex;

@end

@implementation ShopViewController


- (NSArray *)rightArray
{
    if (!_rightArray) {
        _rightArray = @[@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒",@"北大荒"];
    }
    return _rightArray;
}

- (NSArray *)leftArray
{
    if (!_leftArray) {
        _leftArray = [NSArray array];
    }
    return _leftArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
//    [self setupDefaultData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self addTableView];
    [self setNavBarItem];
    UIButton *shopCart = [[UIButton alloc] init];
    [shopCart setFrame:CGRectMake(WIDTH - 60, HEIGHT - 200, 40, 40)];
//        [shopCart setBackgroundColor:[UIColor redColor]];
    [shopCart addTarget:self action:@selector(clickShopCart) forControlEvents:UIControlEventTouchUpInside];
    [shopCart setImage:[UIImage imageNamed:@"gouwuche"] forState:UIControlStateNormal];
    [self.view addSubview:shopCart];
    
    [self loadLeftTableViewData];
}

- (void)clickShopCart {
    NSLog(@"...");
    if ([JudgeUserLogInTool JudgeUserLonInStatus]) {
        
        [self pushForm:self.navigationController pushToAndHidden:[[ShopCartViewController alloc] init]];
    }
}

- (void)setupDefaultData
{
    NSIndexPath *firstIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.leftTableView selectRowAtIndexPath:firstIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
    
}


- (void)addTableView
{
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    CGFloat leftViewW;
    
    if (WIDTH == 320) {
        leftViewW = WEITH/6 *2;
    } else {
        leftViewW= 110;
    }
    
    self.leftTableView = [[UITableView alloc] init];
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    //    self.leftTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    self.leftTableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.rightTableView];
    [self.view addSubview:self.leftTableView];
    
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.delegate = self;
    
    [self.leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view.mas_left);
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.width.mas_equalTo(leftViewW);
        
    }];
    
    [self.rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.leftTableView.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.right.mas_equalTo(self.view.mas_right);
    }];
    
}

#pragma mark >>>>>>dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([tableView isEqual:self.leftTableView]) {
        return 1;
    }else{
        return self.leftArray.count;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.leftTableView]) {
        return self.leftArray.count;
    }else{
        return 1;
    }
}

/**
 *
 *
 *  @param tableView
 *  @param indexPath
 *
 *  @return
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    UITableViewCell *cell = nil;
    //    //左边
    if ([tableView isEqual:self.leftTableView]) {
        
        static NSString *shopLeftCell = @"ShopLeftCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:shopLeftCell];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:shopLeftCell];
            //            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.textLabel.text = [self.leftArray[indexPath.row] objectForKey:@"Name"];
        cell.textLabel.highlightedTextColor = [UIColor redColor];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        cell.selectedBackgroundView.backgroundColor = BACKGROUND_COLOR;
        
        return cell;
        
    }else{
        //    右边
        static NSString *ShopRightCell = @"ShopRightCell";
        rightNewShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ShopRightCell];
        
        if (cell == nil) {
            cell = [[rightNewShopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShopRightCell menuArray:self.rightArray];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        cell.delegate = self;
        cell.superview.superview.backgroundColor = BACKGROUND_COLOR;
        cell.contentView.backgroundColor = BACKGROUND_COLOR;
        
        
        return cell;
        
    }
    
}

#pragma mark >>>>>delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.leftTableView]) {
        NSLog(@"%ld",indexPath.row);
        //
        [self.leftTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
//        [self.rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
        [self loadRightTableViewData:[self.leftArray[indexPath.row] objectForKey:@"Name"]];
        
        
    }else{
        
        NSLog(@"%ld",indexPath.row);
        
        
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) {
        return 0.1;
    }else{
        return TableViewHeaderHeight;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        return 45;
    }else{
        
        return TableViewHeaderHeight *3;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:self.rightTableView]) {
        
        CGFloat margin = 10;
        CGFloat labelHeight = 20;
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.rightTableView.width, TableViewHeaderHeight)];
        bgView.backgroundColor = BACKGROUND_COLOR;
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopStroe_03"]];
        imageView.frame = CGRectMake(margin, margin, self.rightTableView.width - margin *2, TableViewHeaderHeight - margin *2 - labelHeight);
        [bgView addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel *sectionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(margin,CGRectGetMaxY(imageView.frame) + 10, 100, 20)];
        sectionLabel.font = [UIFont systemFontOfSize:13];
        sectionLabel.textColor = [UIColor blackColor];
        sectionLabel.text = self.leftArray[section];
        [bgView addSubview:sectionLabel];
        
        return bgView;
    }else{
        return nil;
    }
}


#pragma mark >>>>>>>rightNewShopDelegate
- (void)rightNewShopDelegateDidSelectedAtIndex:(NSInteger)index
{
    ShoeBagDressViewController * shoes = [[ShoeBagDressViewController alloc] init];
    [self pushForm:self.navigationController pushToAndHidden:shoes];
}


#pragma mark>>>>>>导航栏
- (void)setNavBarItem {
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_1"] highImage:[UIImage imageNamed:@"nav_1"] target:self action:@selector(saoYiSaoBtn) imageEdgeInsets:UIEdgeInsetsMake(0, 10, 15, 0) titleEdgeInsets:UIEdgeInsetsMake(20, -25, 0, 25) title:@"扫一扫"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"nav_3"] highImage:[UIImage imageNamed:@"nav_3"] target:self action:@selector(fuKuanBtn) imageEdgeInsets:UIEdgeInsetsMake(0, 35, 15, -35) titleEdgeInsets:UIEdgeInsetsMake(20, 20, 0, 0) title:@"收款"];
    
    CGRect frame;
    if (WIDTH == 320) {
        frame = CGRectMake(-25, 0, 200, 30);
    } else {
        frame = CGRectMake(0, 0, 200, 30);
    }
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    
    UITextField *searchFeild = [[UITextField alloc] initWithFrame:frame];
    searchFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入商家或者商品名称" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    searchFeild.textColor = [UIColor redColor];
    searchFeild.font = [UIFont systemFontOfSize:14];
    searchFeild.layer.cornerRadius = searchFeild.frame.size.height/2;
    searchFeild.layer.masksToBounds = YES;
    searchFeild.enabled = NO;
    searchFeild.layer.borderWidth = 1;
    searchFeild.layer.borderColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1].CGColor;
    [self setLeftViewWithTextField:searchFeild imageName:@"nav_2"];
    [view addSubview:searchFeild];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = searchFeild.frame;
    [searchBtn addTarget:self action:@selector(searchBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:searchBtn];
    
    self.navigationItem.titleView = view;
}

- (void)searchBut {
    NSLog(@"...");
}

- (void)saoYiSaoBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
        SweepViewController *a = [[SweepViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];
    }
    
}

- (void)fuKuanBtn {
    if ([JudgeUserLogInTool  JudgeUserLonInStatus]) {
        ErWeiMaViewController *a = [[ErWeiMaViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];
    }
    
}

//tabBar 隐藏
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.backgroundColor = [UIColor blackColor];
    textField.alpha = 0.25;
}

- (void)loadLeftTableViewData {
    NSDictionary *params = @{
                             @"ParentSortId":@"0"
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PRODUCTCLASSQUERY" completeBlock:^(id object) {
        
        NSString *string = [object objectForKey:@"BackData"];
        
        self.leftArray = [NetWorkTools arrayWithJsonString:string];
        
        NSLog(@"%@",self.leftArray);
        
        [self.leftTableView reloadData];
        
//        NSLog(@"%@",object)
    }];
}

- (void)loadRightTableViewData:(NSString *)sotrName {
    NSDictionary *params = @{
                             @"SortName":sotrName,
                             @"PageIndex":@"1",
                             @"PageSize":@"10"
                             };

    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PRODUCTLISTQUERY" completeBlock:^(id object) {
        NSLog(@"%@",object);
    }];
}

@end
